#!/bin/bash

# myconfig 全剧配置
[ -f "$HOME/myconfig" ] && . $HOME/myconfig        

declare -A usage
_info () {
    echo -e "\e[32m$@\e[39m"
}
_warn () {
    echo -e "\e[93m$@\e[39m"
}
_err () {
    echo -e "\e[31m$@\e[39m"
}


desc="c:centos u:ubuntu; ci:install on centos; ui:install on centos"
#script begin

#example
#description: 
usage[idocker]="install docker"
usage[irepo]="install aliyun repo"
x(){
     _info "xx"
}
irepo(){
     VERSION=$(cat /etc/centos-release)
     #提取当前系统的版本号
     V_NUM=${VERSION:21:1}

     BASE_REPO="/etc/yum.repos.d/CentOS-Base.repo"
     ALI_REPO="http://mirrors.aliyun.com/repo/Centos-${V_NUM}.repo"
     echo "备份当前软件源..."
     mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
     echo "备份完成: /etc/yum.repos.d/CentOS-Base.repo.backup"

     echo "下载阿里云镜像源..."
     wget -O ${BASE_REPO} ${ALI_REPO} || curl -o ${BASE_REPO} ${ALI_REPO}

     #补丁程序, 防止出现 Couldn't resolve host 'mirrors.cloud.aliyuncs.com' 信息
     sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

     echo "清除缓存..."
     yum clean all
     echo "缓存清除成功,OK"

     echo "生成缓存..."
     yum makecache
     echo "生成缓存成功, OK"

     echo "更新软件..."
     yum update -y
     echo "软件更新完毕, OK"

}
ipip_repo(){
  local confDir=$HOME/.pip
  [ ! -d $confDir ]  && mkdir $confDir
  cat ->$confDir/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF

}
ipip(){
    yum install -y epel-release
    yum install -y python-pip
    ipip_repo
}
rm_sudo_pwd(){
    local sudoersFile=/etc/sudoers
    sudo chmod +w $sudoersFile
    echo "$(whoami) ALL=(ALL:ALL) NOPASSWD: ALL"|sudo tee -a $sudoersFile
    sudo chmod -w $sudoersFile
}
idocker(){
     sudo yum install -y yum-utils

     sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

     sudo yum-config-manager --enable docker-ce-nightly

     sudo yum install docker-ce docker-ce-cli containerd.io -y

     sudo systemctl start docker

     sudo mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
     "registry-mirrors": ["https://m20uyoax.mirror.aliyuncs.com"]#这里改成自己的阿里云镜像加速点
}
EOF
     sudo systemctl daemon-reload
     sudo systemctl restart docker
}






func=$1
isHelp=$2
MY_DIR=$HOME/my
usage["_r"]="show in finder"
usage["_c"]="open in vs code"
_r(){
    open $MY_DIR/script
}
_c(){
    local editor="vim"
    [ ! -z "$(which subl)" ] && editor="subl"
    [ ! -z "$(which code)" ] && editor="code"
    $editor $MY_DIR/script
}
    if [ "$func" = "-" -o -z "$func" ];then
echo -e "$(_info $desc):\n"    
echo -e "$(_info usage):\n"
for key in "${!usage[@]}";do
    echo -e "$(_info $key): \n\t${usage[$key]}"
done

else


if [ "$(type -t $func)" = "function" ];then 

    shift 
    if [ "$isHelp" = "-" ];then 
        #usage="${func}_usage"
        #echo -e "$func usage:\n ${!usage}"
        echo -e "$(_info usage:\\n$func)\n  ${usage[$func]}"
        echo "option help"
    else
       $func "$@"
    fi

fi

fi

